package com.smart.wap.app;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.domain.LoginResult;
import com.smart.common.entities.enums.SmsScene;
import com.smart.common.exception.SmartException;
import com.smart.wap.bodys.LoginBody;
import com.smart.wap.service.AppLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenOAuth2ServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author xiaxuqiu
 * @date 2023/09/10
 */
@Api(tags = "移动端-身份认证")
@RestController
@RequestMapping(SmartConstant.WAP_API_PREFIX + "app/login")
@RequiredArgsConstructor
@Log4j2
public class LoginController {

    private final AppLoginService appLoginService;

    private final WxOpenOAuth2ServiceImpl wxOpenOAuth2Service;

    @PostMapping(value = "getLoginCode")
    @ApiOperation(value = "获取手机登录验证码", notes = SmartConstant.NOT_NEED_TOKEN)
    public AjaxResult<String> getSmsCode(@RequestBody @Validated LoginBody.LoadCaptchaBody body) {
        appLoginService.getSmsCode(SmsScene.MEMBER_LOGIN, body);
        return AjaxResult.success();
    }


    @GetMapping(value = "wxLogin")
    @ApiOperation(value = "微信授权码登录", notes = SmartConstant.NOT_NEED_TOKEN)
    public AjaxResult<LoginResult> getAccessToken(@RequestParam(name = "code") String code) {
        try {
            WxOpenConfigStorage wxOpenConfigStorage = wxOpenOAuth2Service.getWxOpenConfigStorage();
            String appId = wxOpenConfigStorage.getComponentAppId();
            String appSecret = wxOpenConfigStorage.getComponentAppSecret();
            WxOAuth2AccessToken accessToken = wxOpenOAuth2Service.getAccessToken(appId, appSecret, code);
            WxOAuth2UserInfo userInfo = wxOpenOAuth2Service.getUserInfo(accessToken, "zh_CN");
            LoginResult loginResult = appLoginService.actionWxUser(userInfo);
            log.warn("微信授权请求参数:"+loginResult);
            return AjaxResult.data(loginResult);
        } catch (Exception e) {
            throw new SmartException(e.getMessage());
        }

    }

    @PostMapping(value = "refreshToken")
    @ApiOperation(value = "刷新身份令牌", notes = SmartConstant.NOT_NEED_TOKEN + "4002 错误码调用")
    public AjaxResult<LoginResult> refreshToken(@RequestBody @Validated LoginBody.RefreshTokenBody body) {
        LoginResult loginResult = appLoginService.refreshToken(body);
        return AjaxResult.data(loginResult);
    }

    //==================实际处理登录请求的方法===============================


    @PostMapping(value = "smsLogin")
    @ApiOperation(value = "V2手机验证码登录", notes = SmartConstant.NOT_NEED_TOKEN)
    public AjaxResult<LoginResult> actionSmsLogin(@RequestBody @Validated LoginBody.SmsLoginBody body) {
        log.warn("手机验证码登录请求参数:"+body);
        LoginResult loginResult = appLoginService.actionSmsLogin(body);
        return AjaxResult.data(loginResult);
    }


    @PostMapping(value = "oneKeyLogin")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "V2手机号码一键登录", notes = SmartConstant.NOT_NEED_TOKEN)
    public AjaxResult<LoginResult> autoLogin(@RequestBody @Validated LoginBody.OneKeyLoginBody body) {
        LoginResult result = appLoginService.actionOneKeyLogin(body);
        return AjaxResult.data(result);

    }


}
